import { useState } from 'react';
import { getLocalHistories, removeLocalHistories, setLocalHistories } from './storage';
import { uniq } from 'lodash';

/**
 * 搜索历史 hook
 */

export function useSearchHistory() {
  const [historyList, setHistoryList] = useState<string[]>(getLocalHistories());
  const handleAddHistory = (value: string) => {
    const newList = uniq([value, ...historyList]);
    setHistoryList(newList);
    setLocalHistories(newList);
  };
  const handleClearHistory = () => {
    setHistoryList([]);
    removeLocalHistories();
  };
  return {
    historyList,
    handleAddHistory,
    handleClearHistory,
  };
}
